package com.wolfalpha.utron.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemProjectComment;
import com.wolfalpha.utron.service.impl.ProjectServiceImpl;
import com.wolfalpha.utron.utils.StringUtils;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.CircleImageView;
import com.wolfalpha.utron.view.EditAlertDialog;

import java.util.LinkedList;
import java.util.List;

/**
 * 项目列表适配器
 * <p/>
 * Created by luodon on 15/9/1.
 */
public class ItemProjectCommentAdapter extends BaseAdapter {

    private LayoutInflater inflater;

    private Context context;
    private List<ItemProjectComment> projectComments;
    private int pId = 0;

    private LinkedList<String> callsList = new LinkedList<>();
    String call[] = AppContext.CALLS;

    public ItemProjectCommentAdapter(Context context, List<ItemProjectComment> rs, int p) {
        pId = p;
        this.context = context;
        inflater = LayoutInflater.from(context);
        projectComments = rs;
    }

    public void setData(List<ItemProjectComment> rs) {
        if (rs != null) {
            projectComments = rs;
            this.notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return projectComments.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_project_comment, null);
            viewHolder = new ViewHolder();
            viewHolder.civHead = (CircleImageView) convertView.findViewById(R.id.item_civ_project_comment_user_head);
            viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.item_tv_project_comment_user_name);
            viewHolder.tvFloor = (TextView) convertView.findViewById(R.id.item_tv_project_comment_floor);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.item_tv_project_comment_time);
            viewHolder.tvContent = (TextView) convertView.findViewById(R.id.item_tv_project_comment_content);
            viewHolder.ivReply = (ImageView) convertView.findViewById(R.id.item_iv_project_comment);
            viewHolder.llUser1 = (LinearLayout) convertView.findViewById(R.id.item_ll_project_comment_user1);
            viewHolder.llUser2 = (LinearLayout) convertView.findViewById(R.id.item_ll_project_comment_user2);
            viewHolder.tvUserName1 = (TextView) convertView.findViewById(R.id.item_tv_project_comment_user1_name);
            viewHolder.tvUserContent1 = (TextView) convertView.findViewById(R.id.item_tv_project_comment_user1_content);
            viewHolder.tvUserTime1 = (TextView) convertView.findViewById(R.id.item_tv_project_comment_user1_time);
            viewHolder.tvUserName2 = (TextView) convertView.findViewById(R.id.item_tv_project_comment_user2_name);
            viewHolder.tvUserContent2 = (TextView) convertView.findViewById(R.id.item_tv_project_comment_user2_content);
            viewHolder.tvUserTime2 = (TextView) convertView.findViewById(R.id.item_tv_project_comment_user2_time);
            viewHolder.tvMore = (TextView) convertView.findViewById(R.id.item_tv_project_comment_more);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (projectComments != null) {
            //设置控件内容
            viewHolder.tvUserName.setText(projectComments.get(position).getUser_name());
            viewHolder.tvTime.setText(StringUtils.friendly_time(StringUtils.TimeStamp2Date(projectComments.get(position).getTime() * 1000)));
            int f = projectComments.get(position).getFloor();
            viewHolder.tvFloor.setText(f == 0 ? "沙发" : (f == 1 ? "板凳" : "第" + String.valueOf(f) + "楼"));
            viewHolder.tvContent.setText(projectComments.get(position).getContent());
            viewHolder.ivReply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final EditAlertDialog editAlertDialog = new EditAlertDialog(context);
                    editAlertDialog.setHint("输入回复");
                    editAlertDialog.setOnFinishedListener(new EditAlertDialog.OnFinishedListener() {
                        @Override
                        public void finish(final String input) {
                            if (TextUtils.isEmpty(input)) return;
                            ThreadPoolUtils.execute(new Runnable() {
                                @Override
                                public void run() {
                                    new ProjectServiceImpl().doComment(pId, input, projectComments.get(position).getId());
                                    editAlertDialog.dismiss();
                                }
                            });
                        }
                    });
                }
            });

            List<ItemProjectComment.RUser> users = projectComments.get(position).getReplys();
            if (users != null) {
                if (users.size() >= 1) {
                    viewHolder.llUser1.setVisibility(View.VISIBLE);
                    viewHolder.tvUserName1.setText(users.get(0).getUser_name());
                    viewHolder.tvUserContent1.setText(users.get(0).getContent());
                    viewHolder.tvUserTime1.setText(StringUtils.friendly_time(StringUtils.TimeStamp2Date(users.get(0).getTime() * 1000)));
                }
                if (users.size() >= 2) {
                    viewHolder.llUser2.setVisibility(View.VISIBLE);
                    viewHolder.tvUserName2.setText(users.get(1).getUser_name());
                    viewHolder.tvUserContent2.setText(users.get(1).getContent());
                    viewHolder.tvUserTime2.setText(StringUtils.friendly_time(StringUtils.TimeStamp2Date(users.get(1).getTime() * 1000)));
                }
                if (projectComments.get(position).getMore() != 0) {
                    viewHolder.tvMore.setVisibility(View.VISIBLE);
                    viewHolder.tvMore.setText("还有" + String.valueOf(projectComments.get(position).getMore()) + "条回复...");

                }
            }
        }
        Picasso.with(context)
                .load(projectComments.get(position).getUser_head())
                .placeholder(R.mipmap.head_default)
                .into(viewHolder.civHead);

        return convertView;
    }

    /**
     * 控件
     */
    class ViewHolder {
        private CircleImageView civHead;//头像
        private TextView tvUserName;        //用户名
        private TextView tvFloor;        //楼层
        private TextView tvTime;       //时间
        private TextView tvContent;    //内容
        private ImageView ivReply;         //回复按钮

        private LinearLayout llUser1;
        private LinearLayout llUser2;

        private TextView tvUserName1;
        private TextView tvUserContent1;
        private TextView tvUserTime1;
        private TextView tvUserName2;
        private TextView tvUserContent2;
        private TextView tvUserTime2;

        private TextView tvMore;    //更多回复
    }

//    /**
//     * 点击回复事件
//     */
//    public interface OnClickCommentListener {
//        public void click(int rId);
//    }
//
//    private OnClickCommentListener mListener;
//
//    /**
//     * 设置监听器
//     */
//    public void setOnClickCommentListener(OnClickCommentListener listener) {
//        mListener = listener;
//    }
}
